<?php
require 'vendor/autoload.php';

class HTMLCreator {

	private $newsManager;
	private $arrayCleaner;
	private $trendManager;

	function __construct($newsManager, $textCleaner, $trendManager){
		$this->newsManager = $newsManager;
		$this->arrayCleaner = $textCleaner;
		$this->trendManager = $trendManager;
	}

	function createTrendingNewsFile(){

		$newsArray = $this->trendManager->getTrendingNews();
		$trendingNewsFile = 'frontend/trendingNewsView.php';
		$this->generateTrendingNewsHTML($newsArray, $trendingNewsFile);
	}

	function createTrendingWordsFile(){

		$trendingWords = $this->trendManager->getTrendingWords();
		$file = 'frontend/trendingWordsView.php';
		$this->generateTrendingWordsHTML($trendingWords, $file);
	}

	function generateTrendingNewsHTML($trendingNewsArray, $trendingNewsFilePath) {

		$this->cleanFile($trendingNewsFilePath);
		$cnt = 0;
		foreach ($trendingNewsArray as $news) {
			$title = $news->getTitle();
			$image = $news->getImage();
			if ($image != NULL) {
					$htmlImage = "<div style='float:left;display:inline-block;margin-right:10px'><img class='newsImage' src='$image' height='85'/></div>";
			}else{
					$htmlImage = "";
			}
			$filteredTitle = $this->arrayCleaner->removeUnwantedWords($title);
			$filteredTitle = str_replace("'", "", $filteredTitle);
			$link = "'buscar.php?keyword=" . $filteredTitle . "&ordenamiento=peso'>";		
			if ($cnt < 10){
				$firstLine = "\n" . "<div class='newsContainer'id='news$cnt'>";
			}else{
				$firstLine = "\n" . "<div class='newsContainer' style='display:none;' id='news$cnt'>";
			}
			$secondLine = "\n" . $htmlImage. "<div><a class='noticias' href=" . $link . $title ."</div></a>";
			$thirdLine = "\n" . "</div>";
			file_put_contents($trendingNewsFilePath, $firstLine, FILE_APPEND | LOCK_EX);
			file_put_contents($trendingNewsFilePath, $secondLine, FILE_APPEND | LOCK_EX);
			file_put_contents($trendingNewsFilePath, $thirdLine, FILE_APPEND | LOCK_EX);
			$cnt++;
		}
	}

	function generateTrendingWordsHTML($trendingWordsArray, $trendingWordsFilePath) {
		
		$this->cleanFile($trendingWordsFilePath);
		for ($i = 0; $i < 18; $i++) {
			$trendingWordsArray['words'][$i]=ucfirst($trendingWordsArray['words'][$i]);
			if (strlen($trendingWordsArray['words'][$i]) < 13){
				$line = "\n" . "<div class='trendigWordsContainer' id='word$i'><a class='trendigWords' href='buscar.php?keyword=" .$trendingWordsArray['words'][$i]. "&ordenamiento=peso'>" . $trendingWordsArray['words'][$i]. "<br /></a>" . "</div>\n";
			}
			else{
				$line = "\n" . "<div class='trendigWordsContainer' id='word$i'><a class='longTrendigWords' href='buscar.php?keyword=" .$trendingWordsArray['words'][$i]. "&ordenamiento=peso'>" . $trendingWordsArray['words'][$i]. "<br /></a>" . "</div>\n";
			}
			file_put_contents($trendingWordsFilePath, $line, FILE_APPEND | LOCK_EX);
		}
	}

	function cleanFile($file) {
		$empty = "";
		file_put_contents($file, $empty);
	}
}